﻿<#
.SYNOPSIS 
    Returns the status PeopleSoft Application Server.
.DESCRIPTION 
    Use this command to view the status of a PeopleSoft Application Server.
    This command can show the status of the server, clients, or queues on the server.
.NOTES 
    File Name  : Get-PsftStatus.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Get-PsftStatus -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -ServerType AppServer -Domain DOMAIN1 -Target Server
    Show the status of the server processes on DOMAIN1.
.EXAMPLE 
    Get-PsftStatus C:\psoft\PS_HOME C:\psoft\PS_CFG_HOME DOMAIN1 Client
    Show the status of the clients on DOMAIN1.
.PARAMETER PS_HOME
    The full path to the PS_HOME directory for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME directory for this domain.
.PARAMETER ServerType
    The type of server on which to perform the action.
.PARAMETER Domain
    The name of the domain on which to perform the action.
.PARAMETER Target
    One of the following: Server, Client, Queue
    The target of the status request.
#> 
function Get-PsftStatus
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
    [OutputType([Hashtable])]
    Param
    (
        # The full path to the PS_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_HOME directory for this domain.",
                   Position=0)]
        [ValidateScript({Validate_PS_HOME $_})]
        [String]
        $PS_HOME,

        # The full path to the PS_CFG_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_CFG_HOME directory for this domain.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler","SearchServer","WebServer")]
        $ServerType,

        # The name of the domain on which to perform the action.
        # Should be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_Domain $PS_CFG_HOME $ServerType $_})]
        [String]
        $Domain,

        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The target of the status request. E.g. Server",
                   Position=3)]
        [ValidateSet("Server", "Client", "Queue")]
        $Target
    )

    Begin
    {
    }
    Process
    {
		# Set up variables
		$PSAdmin = @{}
		$result = @()
		
        # These commands work for AppServer, PrcsScheduler, and SearchServer
        switch ($Target)
        {
            # Displays the Tuxedo processes and PeopleSoft server processes that are currently running in the specified application server domain.
            'Server' {$Command = "sstatus"}

            # Displays the currently connected users in the PSDMO application server domain.
            'Client' {$Command = "cstatus"}

            # Displays status information about the individual queues for each server process in the PSDMO application server domain.
            'Queue' {$Command = "qstatus"}
        }

        # WebServer has as special command
        if($ServerType -eq "WebServer")
        {
            $Command = "status"
        }

        # Invoke PSAdmin to display the specified status
        $PSAdmin = Invoke-PSAdmin -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain -Command $Command
		$result = $PSAdmin.Result

        $output = @()

        # The results of PSAdmin need to be parsed differently, based on the target.
        switch ($Target)
        {
            'Server' {
                $result | select -Skip 3 | select -Skip 2 -Last $result.count | %{
                    $output += New-Object Object |
                        Add-Member NoteProperty ProgName       (([String]$_.Substring(0,15))  -replace " ") -PassThru | 
                        Add-Member NoteProperty QueueName      (([String]$_.Substring(15,12)) -replace " ") -PassThru |
                        Add-Member NoteProperty GrpName        (([String]$_.Substring(27,10)) -replace " ") -PassThru | 
                        Add-Member NoteProperty ID             ((   [int]$_.Substring(37,6))  -replace " ") -PassThru | 
                        Add-Member NoteProperty RqDone         ((   [int]$_.Substring(43,7))  -replace " ") -PassThru | 
                        Add-Member NoteProperty LoadDone       ((   [int]$_.Substring(50,10)) -replace " ") -PassThru | 
                        Add-Member NoteProperty CurrentService (([String]$_.Substring(61))    -replace " ") -PassThru
                }
            }
            'Client' {
                $result | select -Skip 3 | select -Skip 2 -Last $result.count | %{
                    $output += New-Object Object |
                        Add-Member NoteProperty LMID           (([String]$_.Substring(0,15))  -replace " ") -PassThru | 
                        Add-Member NoteProperty UserName       (([String]$_.Substring(16,15)) -replace " ") -PassThru |
                        Add-Member NoteProperty ClientName     (([String]$_.Substring(32,15)) -replace " ") -PassThru | 
                        Add-Member NoteProperty Time           (([String]$_.Substring(48,8))  -replace " ") -PassThru | 
                        Add-Member NoteProperty Status         (([String]$_.Substring(57,7))  -replace " ") -PassThru | 
                        Add-Member NoteProperty BgnCmmtAbrt    (([String]$_.Substring(65)) -replace " ") -PassThru 
                }
            }
            'Queue' {
                $result | select -Skip 3 | select -Skip 2 -Last $result.count | %{
                    $output += New-Object Object |
                        Add-Member NoteProperty ProgName       (([String]$_.Substring(0,15))  -replace " ") -PassThru | 
                        Add-Member NoteProperty QueueName      (([String]$_.Substring(15,11)) -replace " ") -PassThru |
                        Add-Member NoteProperty NumServe       ((   [int]$_.Substring(27,7)) -replace " ") -PassThru | 
                        Add-Member NoteProperty WkQueued       (([String]$_.Substring(35,9))  -replace " ") -PassThru | 
                        Add-Member NoteProperty NumQueued      ((   [int]$_.Substring(46,8))  -replace " ") -PassThru | 
                        Add-Member NoteProperty AveLen         (([String]$_.Substring(56,8)) -replace " ") -PassThru | 
                        Add-Member NoteProperty Machine        (([String]$_.Substring(64))    -replace " ") -PassThru
                }
            }
        }

        return $output
    }
    End
    {
    }
}